package com.meteor.downloader.videodownloader.activity.downloader

import android.content.Intent
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import coil.load
import coil.transform.RoundedCornersTransformation
import com.meteor.downloader.videodownloader.R
import com.meteor.downloader.videodownloader.activity.MdmBindingActivity
import com.meteor.downloader.videodownloader.adapter.AdapterFormatItem
import com.meteor.downloader.videodownloader.admdm.MdmDealAdUtils
import com.meteor.downloader.videodownloader.databinding.ActivityMdmFormatChooseBinding
import com.meteor.downloader.videodownloader.dismissGoMdm
import com.meteor.downloader.videodownloader.imageCenterCropMdm
import com.meteor.downloader.videodownloader.mdmGetPxDp
import com.meteor.downloader.videodownloader.mdmdata.DataDownloadingMdm
import com.meteor.downloader.videodownloader.utils.MdmTotalUtils
import com.meteor.downloader.videodownloader.utils.dialog.DialogInitMdmLoading
import com.meteor.downloader.videodownloader.utils.value.ParamsMdmUtils.mDownloadingMdmList
import com.meteor.downloader.videodownloader.utils.value.ParamsMdmUtils.mParseLinksList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MdmFormatChooseActivity : MdmBindingActivity<ActivityMdmFormatChooseBinding>() {
    private lateinit var mAdapterFormatItem: AdapterFormatItem
    private val mVideoList = mutableListOf<DataDownloadingMdm>()
    override fun doBackPressed() {
        showBookAdMdm("mdmBack") {
            super.doBackPressed()
        }
    }

    override fun beginViewAndDoMdm() {
        mStarNativeValue = true
        mNativeBookLayout = mMdmBinding.flMdmNativeView
        mMdmBinding.layoutTopSet.tvTitleAll.text = getString(R.string.mdm_download)

        mVideoList.addAll(mParseLinksList)
        if (mVideoList.isNotEmpty()) {
            val first = mVideoList[0]
            mMdmBinding.ivVideoThumb.imageCenterCropMdm(12f)
            mMdmBinding.ivVideoThumb.load(first.videoImageAnyMdm ?: first.videoImageMdm) {
                transformations(RoundedCornersTransformation(12f.mdmGetPxDp()))
                placeholder(R.drawable.mdm_shape_16dp_dcdcdc)
                error(R.drawable.mdm_shape_16dp_dcdcdc)
            }
            val text = first.videoNameMdm.trim()
            if (text.isBlank()) {
                mMdmBinding.tvVideoName.text = getString(R.string.mdm_no_title)
            } else {
                mMdmBinding.tvVideoName.text = text
            }
            first.mSelectMdm = true
        }
        setMdmRecyclerView()
        mMdmBinding.tvDownloadVideo.setOnClickListener {
            showBookAdMdm("", true) {
                for (video in mVideoList) {
                    if (video.mSelectMdm) {
                        mDownloadingMdmList.add(video)
                        startActivity(
                            Intent(
                                this@MdmFormatChooseActivity,
                                MdmDownloadingActivity::class.java
                            )
                        )
                        finish()
                        break
                    }
                }
            }
        }
        lifecycleScope.launch(Dispatchers.Main) {
            delay(288)
            MdmDealAdUtils.loadBookIntersMdm()
        }
    }

    override fun setMdmRecyclerView() {
        super.setMdmRecyclerView()
        mAdapterFormatItem = AdapterFormatItem(this, mVideoList)
        mMdmBinding.rvMdmList.apply {
            layoutManager = LinearLayoutManager(this@MdmFormatChooseActivity)
            adapter = mAdapterFormatItem
        }
    }

    private fun showBookAdMdm(
        type: String, showTip: Boolean = false, statusLook: Boolean = true, mdmBack: () -> Unit
    ) {
        if (statusLook && !MdmTotalUtils.getMdmAppStatus()) {
            mdmBack()
            return
        }
        if (showTip) {
            if (MdmDealAdUtils.getVlaueForInters() != null) {
                showRootBookAdBack(type, statusLook, mdmBack)
                return
            }
            val dialog = DialogInitMdmLoading(this)
            dialog.show()
            MdmDealAdUtils.loadBookIntersMdm(true, statusLook)
            lifecycleScope.launch(Dispatchers.Main) {
                delay(1600)
                if (MdmDealAdUtils.getVlaueForInters() != null) {
                    showRootBookAdBack(type, statusLook, mdmBack)
                    delay(96)
                    dialog.dismissGoMdm()
                    return@launch
                }
                if (MdmDealAdUtils.getVlaueForInters() != null) {
                    delay(1600)
                    showRootBookAdBack(type, statusLook, mdmBack)
                    delay(100)
                    dialog.dismissGoMdm()
                    return@launch
                }
                delay(1600)
                showRootBookAdBack(type, statusLook, mdmBack)
                delay(96)
                dialog.dismissGoMdm()
            }
        } else {
            showRootBookAdBack(type, statusLook, mdmBack)
        }
    }

    override fun getLayoutValue(): ActivityMdmFormatChooseBinding {
        return ActivityMdmFormatChooseBinding.inflate(layoutInflater)
    }
}